<div id="classtree_top" class="classtree-topbar">
 <a class="newdir" href="javascript:;" onclick="jstree_create_organization();return false;" title="新建机构"></a>
 <a class="newdir_1" href="javascript:;" onclick="jstree_create_dir();return false;" title="新建下级部门"></a>
 <a class="newdoc" href="javascript:;" onclick="jstree_create_user();return false;" title="添加用户"></a>
 <!--{if $_G['adminid']==1}--><a class="import " href="{ADMINSCRIPT}?mod=orguser&op=import"  title="导入用户"></a><!--{/if}-->
 <a  class="search" href="javascript:;" onclick="jQuery('.classtree-search').slideDown(300);jQuery('#jstree_search_input').focus();" title="搜索" ></a>
 <a class="guide" href="javascript:;" onclick="show_guide();return false;" title="操作说明"></a>
</div>
<div  class="classtree-search">
    <a href="javascript:;" class="search" onclick="jstree_search();return false" title="搜索"><i class="glyphicon glyphicon-search"></i></a>
    <a href="javascript:;" class="delete" onclick="jstree_search('stop');return false" title="关闭搜索框"><i class="glyphicon glyphicon-remove"></i></a>
	<input id="jstree_search_input" type="text" style="width:240px;height:36px;margin:0 3px;" class="form-control" placeholder="按回车搜索" onkeyup="if(event.keyCode==13){jstree_search()}"  />
</div>
<div id="classtree" class="classtree-container" style="padding:5px 0;overflow:auto;border-top:1px solid #FFF;" ></div>

<script type="text/javascript">
var orgtree=$orgtree;
var ajaxurl='{ADMINSCRIPT}?mod=orguser&op=ajax';
var baseurl='{ADMINSCRIPT}?mod=orguser';
jQuery(document).ready(function(e) {
	
	jQuery("#classtree").jstree({ 
		"core" : {
			"multiple" : true,
			'check_callback' : function (operation, node, node_parent, node_position, more) {
				// operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
				// in case of 'rename_node' node_position is filled with the new node name
				if(node.id=='other') return false;
				if(node_parent.id=='other' && node.type!='user') return false;
				return operation === 'copy_node' && node.type=='organization' ? false : true;
			},
			"themes" : {"responsive" : false,"variant":'large'},
			"data":{
					dataType:'json',
					url : '{ADMINSCRIPT}?mod=orguser&op=ajax&do=getchildren&t='+new Date().getTime(),
					data : function (node) {
					  return { 'id' : node.id };
					}
				  }
		  },
		  "dnd":{
			 "copy":true,
			 "open_timeout":500,
			 "is_draggable":function(node){
				 var inst = jQuery("#classtree").jstree(true);
				 for(var i in node){
					 if(inst.is_disabled(node[i])) return false;
				 }
				 return true;
			 }
		  },
		  
		  "types" : {
				"#" : {
				  "max_children" : -1, 
				  "max_depth" : -1, 
				  "valid_children" : -1
				},
				"organization" : {
				//  "icon" : "jstree-icon-book",
				  "valid_children" : ['user','organization']
				},
				"default" : {
				  "valid_children" : ['user']
				},
				"disabled" : {
				//  "icon" : "jstree-icon-file",
				  "valid_children" : []
				},
				"user" : {
				//  "icon" : "jstree-icon-file",
				  "valid_children" : []
				}
		  },
		  'contextmenu':{
			  'select_node' : false,
			  'show_at_node' : false,
			  'items':{
				  "create_sibing" : {
						"separator_before"	: false,
						"separator_after"	: false,
						"_disabled"			: function(data){
							var inst = jQuery.jstree.reference(data.reference);
							var node=inst.get_node(data.reference);
							if(node.type=='default') return true;
							if(node.type=='user') return true;
							if(inst.is_disabled(node)){
								return true;
							}
							var parent=inst.get_node(node.parent);
							if(inst.is_disabled(parent)){
								return true;
							}
							return false;
						},
						"label"				: "新建同级部门",
						"icon"				: "glyphicon glyphicon-tag",
						"action"			: function (data) {
							var inst = jQuery.jstree.reference(data.reference),
							obj = inst.get_node(data.reference),
							obj1=inst.get_node(obj.parent);
							
							var position=jQuery.inArray(obj.id,obj1.children)+1;
							jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=create',{'forgid':obj.parent,'orgid':obj.id,'t':new Date().getTime()},function(json){
								
								if(!json || json.error){
									showmessage(json.error,'danger',3000,1);
								}else if(json.orgid>0){
									var arr={"id":json.orgid,"text":json.orgname,"type":"organization","icon":(json.forgid>0)?'dzz/system/images/department.png':'dzz/system/images/organization.png'}
									inst.create_node(obj1, arr, position, function (new_node) {
										setTimeout(function () { inst.edit(new_node); },0);
									});
								}
							},'json');
						}
					},
					
				  "create" : {
						"separator_before"	: false,
						"separator_after"	: false,
						"_disabled"			: function(data){
							var inst = jQuery.jstree.reference(data.reference);
							var node=inst.get_node(data.reference);
							var parent=inst.get_node(node.parent);
							if(node.type=='default') return true;
							if(node.type=='user') return true;
							if(inst.is_disabled(node)){
								return true;
							}
							return false;
						},
						"label"				: "新建下级部门",
						"icon"				: "glyphicon glyphicon-tags",
						"action"			: function (data) {
							var inst = jQuery.jstree.reference(data.reference),
							obj = inst.get_node(data.reference);
							jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=create',{'forgid':obj.id,'t':new Date().getTime()},function(json){
								if(!json || json.error){
									showmessage(json.error,'danger',3000,1);
								}else if(json.orgid>0){
									var arr={"id":json.orgid,"text":json.orgname,"type":"organization","icon":(json.forgid>0)?'dzz/system/images/department.png':'dzz/system/images/organization.png'};
									
									inst.create_node(obj, arr, "first", function (new_node) {
										setTimeout(function () { inst.edit(new_node); },0);
									});
								}
							},'json');
						}
					},
					
					"rename" : {
						"separator_before"	: false,
						"separator_after"	: false,
						"_disabled"			: function(data){
							var inst = jQuery.jstree.reference(data.reference);
							var node=inst.get_node(data.reference);
							if(node.type=='default') return true;
							if(inst.is_disabled(node)){
								return true;
							}
							if(node.type=='user') return true;
							
							return false;
						},
						"label"				: "重命名",
						/*
						"shortcut"			: 113,
						"shortcut_label"	: 'F2',*/
						"icon"				: "glyphicon glyphicon-leaf",
						
						"action"			: function (data) {
							
							var inst = jQuery.jstree.reference(data.reference),
								obj = inst.get_node(data.reference);
								inst.edit(obj);
									
						}
					},
					"remove" : {
						"separator_before"	: false,
						"icon"				: false,
						"separator_after"	: false,
						"_disabled"			: function(data){
							var inst = jQuery.jstree.reference(data.reference);
							var node=inst.get_node(data.reference);
							if(node.type=='default') return true;
							if(inst.is_disabled(node)){
								return true;
							}
							var parent=inst.get_node(node.parent);
							if(inst.is_disabled(parent)){
								return true;
							}
							
							return false;
						},
						"label"				: "删除",
						"icon"				: "glyphicon glyphicon-remove",
						"action"			: function (data) {
							var inst = jQuery.jstree.reference(data.reference),
								obj = inst.get_node(data.reference);
								
								//判断是否为相同类型的多选和相同部门的
								var msg='';
								var nodes=[];
								if(inst.is_selected(obj)){
									var nodes=inst.get_selected(true);
									var type=null;
									var parent=null;
									for(var i in nodes){
										if(!type) type=nodes[i].type;
										else if(type!=nodes[i].type){
											msg='请选择相同类型的节点';
											break;
										}
										if(!parent) parent=nodes[i].parent;
										else if(parent!=nodes[i].parent){
											msg='请选择相同部门的节点';
											break;
										}
									}
								}else{
									var nodes=[obj];	
								}
								if(msg){
									 showmessage(msg,'danger',3000,1);
									 return;
								}
								if(obj.parent=='other'){
									var uids=[];
									for(var i in nodes){
										uids.push(nodes[i].li_attr.uid);
									}
									var msg='您确定要彻底删除此用户(用户的所有数据和文件都会彻底删除）吗？';
									
									var data={'uids':uids,'forgid':obj.parent,'type':'user','realdelete':'1'};
								}else{
									if(obj.type=='user'){
										var uids=[];
										for(var i in nodes){
											uids.push(nodes[i].li_attr.uid);
										}
										
										var msg='此处删除，仅从部门中移除此用户，移除后您可能没有操作此用户的权限，您确定要移除此用户吗？';
										var data={'uids':uids,'forgid':obj.parent,'type':'user'};
										
									}else{
										if(nodes.length>1) {
											showmessage('机构或部门不支持批量删除','danger',3000,1);
									 		return;
										}
										var msg='删除部门前，必须先删除此部门的所有下级部门，并且删除共享目录中的文件，您确定要删除此部门吗？';
										var data={'orgid':obj.id,'forgid':obj.parent}
									}
								}
								if(confirm(msg)){
									jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=delete&t='+new Date().getTime(),data,function(json){
										if(json.error){
											showmessage(json.error,'danger',3000,1);
										}else{
											if(inst.is_selected(obj)) {
												inst.delete_node(inst.get_selected(true));
											}
											else {
												inst.delete_node(obj);
											}
											if(obj.type=='user'){
												var parent=inst.get_node('other');
												inst.refresh_node(parent);
											}
										}
										
									},'json');
								}
						}
					}
			  }
		  },
		   "search":{ 
		   		 "show_only_matches":true
				 ,"fuzzy":false
				 ,"ajax":{'url' : '{ADMINSCRIPT}?mod=orguser&op=ajax&do=search','dataType':'json'}
		   },
		  "plugins" : ["unique","contextmenu", "dnd", "types","search"]
		// List of active plugins
		
		});

	 jQuery("#classtree").on('ready.jstree',function(e){
		 var inst = jQuery("#classtree").jstree(true);
		 for(var i in orgtree){
			if(document.getElementById(orgtree[i][0])) open_node_dg(inst,document.getElementById(orgtree[i][0]),orgtree[i]);
		 }
	 });
	 jQuery(document).on('touchend','#classtree .jstree-anchor',function(){
		 var inst = jQuery("#classtree").jstree(true);
		 var node=jQuery(this).closest('.jstree-node');
		 inst.select_node(node);
		 return false;
	 });
	jQuery("#classtree").on('select_node.jstree',function(e,data){
		var inst=jQuery("#classtree").jstree(true);
		inst.open_node(data.node);
		if(data.selected.length>1) return;
		if(data.node.id=="other") return;
		if(data.node.type=='user'){
			showDetail(data.node.li_attr.uid,'user');
		}else{
			showDetail(data.node.id,'organization');
		}
	 });
	 
	jQuery("#classtree").on('load_node.jstree',function(e,data){ //设置空节点为leaf
		var inst=jQuery("#classtree").jstree(true);
		if(data.node.children.length<1){
			jQuery('#'+data.node.id).removeClass('jstree-closed').addClass('jstree-leaf');;
		}
	 });
	
	 jQuery(document).on('dnd_move.vakata',function(e,data){
		 if(jQuery(data.event.target).closest('.moderators-acceptor').length){
			 jQuery('.moderators-acceptor').addClass('hover').find('img').attr('src','avatar.php?uid='+jQuery(data.element).parent().attr('uid')+'&size=middle');
		 }else{
			 jQuery('.moderators-acceptor').removeClass('hover');
		 }
	 });
	  jQuery(document).on('dnd_stop.vakata',function(e,data){
		if(!jQuery(data.event.target).closest('.moderators-acceptor').length) return;
		var uid=jQuery(data.element).parent().attr('uid');
		var orgid= jQuery('.moderators-acceptor').attr('orgid');
		moderator_add(orgid,uid);
	 });
	 jQuery("#classtree").on('move_node.jstree',function(e,data){
		 if(jQuery(e.target).closest('.moderators-acceptor').length) return;
		 var inst=jQuery("#classtree").jstree(true);
		 var node=data.node;
		 var parent=inst.get_node(data.parent);
		 var post={};var needsave=0;
		 if(node.type=='user' && data.parent!=data.old_parent){//移动用户
			  post.uid=node.li_attr.uid;
			  post.forgid=data.old_parent;
			  post.orgid=data.parent;
			  post.type='user';
			  needsave=1;
		 }else{
			 post.orgid=node.id;
			 post.forgid=data.parent;
			 post.position=data.position;
			 needsave=1;
		 }
		 if(needsave){
			jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=move&t='+new Date().getTime(),post,function(json){
				if(json.error){
					showmessage(json.error,'danger',3000,1,null,function(){
						window.location.reload();
					});
				}
			},'json');
		 }
	 });
	 jQuery("#classtree").on('copy_node.jstree',function(e,data){
		 if(jQuery(e.target).closest('.moderators-acceptor').length) return;
		 var inst=jQuery("#classtree").jstree(true);
		 var node=data.node;
		 var parent=inst.get_node(data.parent);
		 var post={};var needsave=0;
		 if(node.type=='user' && data.parent!=data.old_parent){//移动用户
			  post.uid=node.li_attr.uid;
			  post.forgid=data.old_parent;
			  post.orgid=data.parent;
			  post.type='user';
			  post.copy=1;
			  needsave=1;
		 }else{
			 return;
		 }
		 if(needsave){
			jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=move&t='+new Date().getTime(),post,function(json){
				
			},'json');
		 }
	 });
	 jQuery("#classtree").on('open_node.jstree',function(e,data){
		 //jQuery("#"+data.node.id+" .jstree-anchor").addTouch();
	 });
	jQuery("#classtree").on('rename_node.jstree',function(e,data){
		var inst = jQuery("#classtree").jstree(true);
		var obj = inst.get_node(data.node);
		if(data.text==''){
			inst.set_text(obj,data.old);
			return;
		}
		jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=rename&t='+new Date().getTime(),{'orgid':data.node.id,text:data.text},function(json){
			if(!json || json.error){
					obj.text=data.old;
					inst.set_text(obj,data.old);
				if(json.error) showmessage(json.error,'danger',3000,1);	
			}
		},'json');
	 });
	// jQuery("#classtree").on('create_node.jstree',function(e,data){});
	
});

</script>